package com.wenwei.sharebox.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wenwei.sharebox.R;
import com.wenwei.sharebox.base.BaseActivity;
import com.wenwei.sharebox.bean.RechargeInterageBean;
import com.wenwei.sharebox.net.ApiManager;
import com.wenwei.sharebox.net.BaseSubscriber;
import com.wenwei.sharebox.utils.UserStringUtils;
import com.wenwei.sharebox.view.PayPopupWindow;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/7/10.
 */
public class TopUpActivity extends BaseActivity {
    @Bind(R.id.base_title)
    TextView baseTitle;
    @Bind(R.id.price_et)
    EditText priceEt;
    @Bind(R.id.commit_btn)
    TextView commitBtn;
    @Bind(R.id.main_ll)
    LinearLayout contentPll;

    private int mType = -1;//1:积分充值 2：缴纳押金


    @Override
    public int setLayoutResourceId() {
        return R.layout.activity_top_up;
    }

    @Override
    protected void init() {
        super.init();
        mType = getIntent().getIntExtra("type",0);
    }

    @Override
    protected void initView() {
        super.initView();
        if(mType == 1){
            baseTitle.setText("充值积分");
        }else if (mType == 2){
            baseTitle.setText("缴纳押金");
            commitBtn.setText("确认缴纳");
        }

        priceEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(!priceEt.getText().toString().equals("")){
                    commitBtn.setEnabled(true);
                    commitBtn.setBackgroundResource(R.drawable.bc_share_select);
                }else{
                    commitBtn.setEnabled(false);
                    commitBtn.setBackgroundResource(R.drawable.bc_click_false);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
    }

    @OnClick({R.id.base_back, R.id.commit_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.base_back:
                finish();
                break;
            case R.id.commit_btn:
//                showToast("充值成功");
//                finish();
                if(mType == 1){
                    rechargeIntegral();
                }
                break;
        }
    }

    PayPopupWindow payPopupWindow;
    //积分充值
    private BaseSubscriber<RechargeInterageBean> rechargeIntegralSub;
    private void rechargeIntegral(){
        rechargeIntegralSub = new BaseSubscriber<RechargeInterageBean>(mContext,true,"正在生成订单，请稍后...") {
            @Override
            public void onSuccess(RechargeInterageBean rechargeInterageBean) {
                payPopupWindow = new PayPopupWindow(TopUpActivity.this, contentPll, new PayPopupWindow.CallBack() {
                    @Override
                    public void onPayClick() {

                    }
                },rechargeInterageBean.getOrderNumber());
            }
        };
        ApiManager.getInstance().insetIntegralRecharge(rechargeIntegralSub, UserStringUtils.getUserId(mContext),Float.parseFloat(priceEt.getText().toString()));
    }

    //缴纳押金
    private void payDeposit(){

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        payPopupWindow = null;
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(String event) {
        showToast("支付回调："+event);
        payPopupWindow.dismiss();
        goActivity(mContext, PaySuccessActivity.class);   //
    }

}
